package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.TalentsDetailsActivity;
import com.ucomm.talent.adapter.TalentsInfoListAdapter;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.TalentsInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 高级人才
 * @author Administrator
 *
 */
public class TalentsActivity extends Activity implements IHeaderBarClickListener{
	
	private XListView listView;
	private MainHeaderBar mainHeaderBar;
	private CustomProgressDialog customProgressDialog;
	private SearchManager searchManager;
	private TalentsInfoListAdapter adapter;
	private ArrayList<TalentsInfo> data; 
	private String url = null;
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case AppConstants.MsgStatus.MSG_ERROR:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
                ((AppException)msg.obj).makeToast(TalentsActivity.this);
                break;
            case AppConstants.MsgStatus.MSG_SUCCUSS:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
                //放置数据
                data.addAll((ArrayList<TalentsInfo>) msg.obj);
                if(adapter==null){
	                adapter = new TalentsInfoListAdapter(TalentsActivity.this, data);
	                listView.setAdapter(adapter);
                }else{
                	adapter.notifyDataSetChanged();
                }
                listView.setOnItemClickListener(new ListItemListener());
                //上拉加载更多
                listView.setPullLoadEnable(new IXListViewLoadMore() {
                    @Override
                    public void onLoadMore() {
                    	if(AppConfig.CURR_PAGE !=0){
                    		int i = AppConfig.CURR_PAGE;
                    		if(i<AppConfig.PAGE_COUNT){
                    			AppConfig.CURR_PAGE += 1;
                    			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
                            	searchManager.searchDataFromNetwrok(pageUrl);
                    		}else{
                    			listView.disablePullLoad();
                    		}
                    	}
                    }
                });
                //下拉刷新
                listView.setPullRefreshEnable(new IXListViewRefreshListener() {
                    @Override
                    public void onRefresh() {
                    	searchManager.searchDataFromNetwrok(url);
                    	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
                    }
                });
                
                break;
            case AppConstants.MsgStatus.MSG_LOADING:
                if (customProgressDialog != null && !customProgressDialog.isShowing())
                    customProgressDialog.show();
                break;
            default:
                break;
			}
			super.handleMessage(msg);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.talents_activity_layout);
		initView();
		searchManager = new SearchManager(this, mHandler);
		Intent i = getIntent();
	    String otherUrl = i.getStringExtra("url");
	    if (otherUrl!=null){
	    	url = otherUrl;
	    }else{
	    	url = AppConstants.TALENTS;
	    }
		searchManager.searchDataFromNetwrok(url);
	}
	 private void initView(){
        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
        mainHeaderBar.setBarClickListener(this);
        customProgressDialog = new CustomProgressDialog(this);

        listView = (XListView) findViewById(R.id.custom_list);
        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
        data = new ArrayList<TalentsInfo>();
    }

	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ArrayList<TalentsInfo> mData = adapter.getmData();
			TalentsInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(TalentsActivity.this, TalentsDetailsActivity.class);
			startActivity(i);
		}
		
	}
	@Override
	public void onBackButtOnClick(View view) {
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}

}
